import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as IndiDiviService from '../../../services/AccountDivi';
const daoName = "VIndiLoanDivi";
const ButtonGroup = Button.Group;
const columns = [
  { title: '员工号', dataIndex: 'EMNO', key: 'EMNO', width: 110, sorter: true, dispaly: true, filter: true },
  { title: '员工ID', dataIndex: 'EMID', key: 'EMID', width: 110, sorter: true, dispaly: false, filter: false },
  { title: '员工姓名', width: 110, sorter: true, dataIndex: 'NAME', key: 'NAME', dispaly: true, filter: true },
  { title: '所属机构', width: 110, sorter: true, dataIndex: 'OrganName', key: 'OrganName', dispaly: true, filter: true },
  { title: '分户日期', width: 110, sorter: true, key: 'DSDT', caption: "DSDT", render: data => datetime.format(data.DSDT, "yyyy-MM-dd"), dispaly: true, filter: true, canEdit: true },
  { title: '终止日期', width: 110, sorter: true, key: 'WODT', caption: "WODT", render: data => datetime.format(data.WODT, "yyyy-MM-dd"), dispaly: true, filter: false },
  { title: '分成比例(%)', width: 160, sorter: true, dataIndex: 'DSVL', key: 'DSVL', dispaly: true, filter: true, render: data => data * 100 },
];
const controls = [
  { name: 'EMNO', required: true, type: 'text', min: 1, max: 50, label: '员工号', grid: 12 / 24 },
  { name: 'NAME', required: true, type: 'text', label: '员工姓名', min: 1, max: 50, grid: 12 / 24 },
  { name: 'OrganName', required: true, type: 'text', label: '所属机构', grid: 12 / 24 },
  { name: 'DSVL', required: true, type: 'number', label: '分成比例', grid: 12 / 24, dataType: "number" },
  { name: 'DSDT', required: true, type: 'date', label: '分户日期', grid: 12 / 24, dataType: "date" },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class INDI_LOAN_DIVIForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const l1 = nextProps.masterSelectedRowKeys.length
    const l2 = this.props.masterSelectedRowKeys.length
    let k1 = -1
    let k2 = -1
    if (l1 > 0) {
      k1 = nextProps.masterSelectedRowKeys[l1 - 1]
    }
    if (l2 > 0) {
      k2 = this.props.masterSelectedRowKeys[l2 - 1]
    }
    if (k1 !== k2) {
      let seeks = [];
      let whereItems = [];
      let WhereCauses = [];
      whereItems.push({
        FieldName: "ACID",
        Operator: "=",
        FieldValues: '' + k1
      })
      whereItems.push({
        FieldName: "ISWO",
        Operator: "=",
        FieldValues: 'false'
      })
      WhereCauses.push({
        WhereItems: whereItems,
        RelationSign: "AND"
      })
      seeks = WhereCauses;
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 20, seeks }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  del(){
    let len = this.props.selectedRowKeys.length;
    if (len <= 0) {
        message.error("请选择要删除的数据行！")
        return
    }
    let key = this.props.selectedRowKeys;
    let id = key[0]
    Modal.confirm({
        title: '提示',
        content: `你确认要删除该认定关系吗？`,
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
            IndiDiviService.DeleteINDILOAN({ id }
            ).then(function (data) {
                if (data === 'OK') {
                    message.success('删除认定关系成功！')
                } else {
                    message.error('删除认定关系失败！' + data)
                    return;
                }
                this.props.dispatch({
                    type: `${"INDILOAN"}/setSelectedRowKeys`,
                    payload: { selectedRowKeys: [] }
                });
            }.bind(this)).catch(function (ex) {
                console.log('请求出错', ex)
            });
        }
    });   
}

  render() {
    let { showEdit, record, offsetHeight, masterSelectedRowKeys, permID } = this.props
    let table2Height = (offsetHeight - 128) * 0.5 - 70
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -10, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
          <ButtonGroup>
            【账户认定情况】
               <Button onClick={this.del.bind(this)} type={"dashed"} icon={"close-circle"} size={"small"}>删除</Button>
          </ButtonGroup>
          </div>
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} height={table2Height} canExport={false} permID={permID} />
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const masterSelectedRowKeys = state['INDILOAN'].selectedRowKeys;
  const { offsetHeight } = state.menus
  return {
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    masterSelectedRowKeys
  }
}
INDI_LOAN_DIVIForm = connect(mapStateToProps)(INDI_LOAN_DIVIForm)
export default INDI_LOAN_DIVIForm;

let render = (props) => React.createElement(INDI_LOAN_DIVIForm, props);
let f = new Factory()
f.set("INDI_LOAN_DIVIForm", render)
